﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UTC_Project_Manager.Model.Infrastructure;

namespace UTC_Project_Manager.Controls
{
    /// <summary>
    /// Interaction logic for ReunionUpperControl.xaml
    /// </summary>
    public partial class ReunionUpperControl : UserControl
    {
        private Meeting formerMeeting;

        public ReunionUpperControl()
        {
            InitializeComponent();
            ReunionListControl.newReunionEvent += new ReunionListControl.ReunionEvent(ReunionListControl_newReunionEvent);
            ReunionListControl.newReunionSelectedEvent += new ReunionListControl.ReunionSelectedEvent(ReunionListControl_newReunionSelectedEvent);
            NewReunionControl.emptyDescription += new NewReunionControl.EmptyDescriptionEvent(NewReunionControl_emptyDescription);
            NewReunionControl.newReunionCreatedEvent += new NewReunionControl.NewReunionEvent(NewReunionControl_newReunionCreatedEvent);
            NewReunionControl.newStartMeetingEvent += new NewReunionControl.StartMeetingEvent(NewReunionControl_newStartMeetingEvent);
            CurrentMeetingControl.endOfMeetingEvent += new CurrentMeetingControl.EndOfMeetingEvent(CurrentMeetingControl_endOfMeetingEvent);
        }

        private void CurrentMeetingControl_endOfMeetingEvent(Meeting meeting)
        {
            this.reunionListControl.IsEnabled = true;
            this.reunionListControl.futureMeetingsListBox.SelectedIndex = -1;
            this.reunionListControl.currentMeetingsListBox.SelectedIndex = -1;
            this.reunionListControl.finishedMeetingsListBox.SelectedItem = meeting;
            
            
        }

        private void NewReunionControl_newStartMeetingEvent()
        {
            Meeting currentMeeting = null;
            if (this.reunionListControl.currentMeetingsListBox.SelectedIndex != -1)
                currentMeeting = this.reunionListControl.currentMeetingsListBox.SelectedItem as Meeting;
            else if (this.reunionListControl.futureMeetingsListBox.SelectedIndex != -1)
                currentMeeting = this.reunionListControl.futureMeetingsListBox.SelectedItem as Meeting;

            if (currentMeeting != null)
            {

                //NewReunionControl formerMeeting = this.descriptionGroupBox.Content as NewReunionControl;
                //CurrentMeetingControl currentMeeting = new CurrentMeetingControl();
                //currentMeeting.titleLabel.Content = formerMeeting.titleTextBox.Text;
                //currentMeeting.descriptionTextBlock.Content = formerMeeting.descriptionTextBox.Text;
                //currentMeeting.meetingDateLabel.Content = DateTime.Now.ToShortDateString();// formerMeeting.meetingDatePicker.SelectedDate.Value.ToShortDateString();
                //currentMeeting.meetingTimeLabel.Content = DateTime.Now.ToShortTimeString();
                //currentMeeting.pointsTextBox.Content = formerMeeting.pointsTextBox.Text;
                //this.reunionListControl.IsEnabled = false;
                //this.descriptionGroupBox.Content = currentMeeting;
                
                CurrentMeetingControl currentMeetingControl = new CurrentMeetingControl();
                currentMeetingControl.titleLabel.Content = currentMeeting.Title;
                currentMeetingControl.descriptionTextBlock.Content = currentMeeting.Description;
                currentMeetingControl.meetingDateLabel.Content = DateTime.Now.ToShortDateString();// formerMeeting.meetingDatePicker.SelectedDate.Value.ToShortDateString();
                currentMeetingControl.meetingTimeLabel.Content = DateTime.Now.ToShortTimeString();
                currentMeeting.Date = DateTime.Now;
                currentMeetingControl.pointsTextBox.Content = currentMeeting.Points;
                currentMeetingControl.CurrentMeeting = currentMeeting;
                this.reunionListControl.IsEnabled = false;
                this.descriptionGroupBox.Content = currentMeetingControl;
            }
        }

       

        private void NewReunionControl_newReunionCreatedEvent(Meeting meeting)
        {
            this.formerMeeting = meeting ;
        }

        private void NewReunionControl_emptyDescription(Boolean isNewMeeting)
        {
            if (isNewMeeting)
                this.descriptionGroupBox.Content = null;
            else
            {
                //this.descriptionGroupBox.Content = formerNewReunionControl;
                ReunionListControl_newReunionSelectedEvent(formerMeeting);
            }
        }

        private void ReunionListControl_newReunionSelectedEvent(UTC_Project_Manager.Model.Infrastructure.Meeting meeting)
        {
            if (!meeting.IsOver)
            {
                NewReunionControl reunionControl = new NewReunionControl();
                reunionControl.IsInit = true;
                reunionControl.startButton.Visibility = Visibility.Visible;
                reunionControl.titleTextBox.Text = meeting.Title;
                reunionControl.descriptionTextBox.Text = meeting.Description;
                reunionControl.pointsTextBox.Text = meeting.Points;
                reunionControl.meetingDatePicker.SelectedDate = meeting.Date;
                reunionControl.meetingTimePicker.SelectedTime = meeting.Date.TimeOfDay;
                //foreach (Member member in meeting.Invited)
                //{

                //    CheckBox cb = reunionControl.membersListBox.ItemContainerGenerator.ContainerFromItem(reunionControl.membersListBox).(member.ToString().Replace(" ", "")) as CheckBox;
                //    cb.IsChecked = true;

                //}

                
                reunionControl.IsFinalized = true;
                reunionControl.CheckMembers(meeting.Invited);
                reunionControl.IsInit = false;
                reunionControl.cancelButton.IsEnabled = false;

                //we save the control so that we can revert the changes
                formerMeeting = meeting;
                this.descriptionGroupBox.Content = reunionControl;
            }
            else
            {
                OverMeetingControl overMeeting = new OverMeetingControl();
                overMeeting.titleLabel.Content = meeting.Title;
                overMeeting.descriptionLabel.Content = meeting.Description;
                overMeeting.lengthLabel.Content = meeting.Length;
                overMeeting.dateLabel.Content = meeting.ToString();
                overMeeting.pointsLabel.Content = meeting.Points;
                overMeeting.notesLabel.Content = meeting.Notes;
                overMeeting.invitedLabel.Content = getInvited(meeting);
                overMeeting.attendedLabel.Content = getAttended(meeting);
                
                this.descriptionGroupBox.Content = overMeeting;
            }

        }

        private void ReunionListControl_newReunionEvent()
        {
            this.descriptionGroupBox.Content = new NewReunionControl();

        }

        private string getAttended(Meeting meeting)
        {
            String st = "";
            foreach (Member m in meeting.Attended)
                st += "+" + m.ToString() +"\r\n";
            return st;


        }

        private string getInvited(Meeting meeting)
        {
            String st = "";
            foreach (Member m in meeting.Invited)
                st += "+" + m.ToString() +"\r\n";
            return st;


        }
    }
}
